﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：审计模型
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                    page.search2({keyword: keyword});
                }
            });
            $('#category').lrDataItemSelect({
                code: 'AUDIT_MODEL_CATEGORYS', maxHeight: 100, placeholder: "==请选择预警分类==",
                select: function (item) {
                    page.search();
                    page.search2();
                }
            });
            // 公司选择
            $('#companyId').lrselect({
                type: 'tree',
                placeholder: '==请选择单位==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function(item) {
                    var keyword = $("#txt_Keyword").val();
                }
            });
            $('#companyId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree_all?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
                page.search2({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 执行分析
            $('#lr_excute').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要执行分析？', function (res) {
                        if (res) {
                            learun.postForm2(config.base_server() + 'auditmodels/analyse?modelId=' + keyValue + '&access_token=' + config.getToken().access_token, {}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }

            });
            // 预览结果集
            $('#lr_preview').on('click', function () {
                var code = $('#gridtable').jfGridValue('code');
                var keyValue = $('#gridtable').jfGridValue('id');
                var modelName = $('#gridtable').jfGridValue('name');
                var defaultKjnd = $('#gridtable').jfGridValue('defaultKjnd');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'preview_result' + keyValue,
                        title: '预览结果集',
                        url: config.webportal_server + 'pages/finance/audit_rule/preview_result/result' + code + '.html?modelId=' + keyValue + '&defaultKjnd=' + defaultKjnd + '&access_token=' + config.getToken().access_token,
                        width: 1000,
                        height: 800,
                        maxmin: true,
                        btn: null
                    });
                }
            });
            // 规则参数设置
            $('#lr_rule').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑规则参数',
                        url: config.webportal_server + 'pages/finance/audit_rule/form.html?access_token=' + config.getToken().access_token,
                        width: 550,
                        height: 580,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 告警通知设置
            $('#lr_scope').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑预警通知',
                        url: config.webportal_server + 'pages/finance/audit_rule/warn_scope/form.html?modelId=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 780,
                        height: 580,
                        btn: null,
                        callBack: function (id) {

                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'auditmodelrules/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'companyexcel/export?access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function () {
            $('#gridtable_category').jfGrid({
                url: config.base_server() + 'auditmodelwarns/stat?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "单位", name: "companyName", width: 180, align: "left"},
                    {label: "账务处理", name: "finance", width: 60, align: "center",statistics: true},
                    {label: "支付管理", name: "pay", width: 60, align: "center",statistics: true},
                    {label: "资金效益", name: "reimburse", width: 60, align: "center",statistics: true},
                    {label: "固定资产", name: "asset", width: 60, align: "center",statistics: true},
                    {label: "政府采购", name: "purchase", width: 60, align: "center",statistics: true},
                    {label: "公车管理", name: "car", width: 60, align: "center",statistics: true},
                    {label: "其他", name: "other", width: 60, align: "center",statistics: true},
                    {label: "单位id", name: "companyId", width: 1, align: "center",hidden: true},
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable_category').jfGridGet('rowdata');
                    var companyId = row.companyId;
                    page.search2({companyId: companyId});
                },
                isTree: false,
                mainId: 'id',
                isPage: false,
                reloadSelected: true,
            });
            $('#gridtable_detail').jfGrid({
                url: config.base_server() + 'auditmodelwarns/groupByModelId?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "模型名称", name: "codeAndName", width: 280, align: "left"},
                    {label: "预警分类", name: "categoryName", width: 110, align: "center"},
                    {
                        label: "市院", name: "c00", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "天河", name: "c01", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "海珠", name: "c02", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "越秀", name: "c03", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "荔湾", name: "c04", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "白云", name: "c05", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "番禺", name: "c06", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "黄埔", name: "c07", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "南沙", name: "c08", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "花都", name: "c09", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "增城", name: "c10", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {
                        label: "从化", name: "c11", width: 40, align: "center",statistics: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-warning\" style='color:red;'></i>" : "";
                        }
                    },
                    {label: "编号", name: "code", width: 1, align: "center",hidden: true}
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable_detail').jfGridGet('rowdata');
                    var keyValue = selectedRow.modelId;
                    var modelName = selectedRow.name;
                    var defaultKjnd = selectedRow.defaultKjnd;
                    var categoryRow = $('#gridtable_category').jfGridGet('rowdata');
                    var companyId = categoryRow.companyId;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'preview_result' + keyValue,
                            title: '预览结果集',
                            url: config.webportal_server + 'pages/finance/audit_rule/preview_result/result' + keyValue + '.html?modelId=' + keyValue + '&defaultKjnd=' + defaultKjnd + '&companyId=' + companyId + '&access_token=' + config.getToken().access_token,
                            width: 1000,
                            height: 800,
                            maxmin: true,
                            btn: null
                        });
                    }
                },
                isTree: false,
                mainId: 'id',
                isPage: false,
                reloadSelected: true,
            });
            page.search();
            page.search2();
        },
        search: function (param) {
            param = param || {};
            var category = $("#category").lrselectGet();
            param.category = category;
            $('#gridtable_category').jfGridSet('reload', param);
        },
        search2: function (param) {
            param = param || {};
            var category = $("#category").lrselectGet();
            param.category = category;
            $('#gridtable_detail').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


